package com.zimmur.app.mocolink.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.zimmur.app.mocolink.dao.BannerMapper;
import com.zimmur.app.mocolink.domain.model.BackgroundImage;
import com.zimmur.app.mocolink.domain.model.Banner;
import com.zimmur.app.mocolink.domain.model.BannerExample;
import com.zimmur.app.mocolink.domain.vo.BannerInfo;
import com.zimmur.app.mocolink.service.IBannerService;
import com.zimmur.app.mocolink.service.RedisCacheUtil;

@Service
public class BannerServiceImpl implements IBannerService {
 
	private static final Logger log = LoggerFactory.getLogger(BannerServiceImpl.class); 
	@Autowired
	private BannerMapper bannerMapper;
	@Autowired
	private RedisCacheUtil<String> redisUtil;
	
	private static final String redis_key = "mocolink_banner_";
	
	@Override
	public List<BannerInfo> selectByShowMethod(int showMethod) {
		String key=redis_key+showMethod;
		List<BannerInfo> list;
		try {
			boolean isExist=redisUtil.exists(key);
			if(isExist){
				String json = redisUtil.getCacheString(key);
				list=JSONObject.parseArray(json, BannerInfo.class);
			}else{
				synchronized (redis_key) {
					isExist=redisUtil.exists(key);
					if(isExist){
						String json = redisUtil.getCacheString(key);
						list=JSONObject.parseArray(json, BannerInfo.class);
					}else{
						list = bannerMapper.selectByShowMethod(showMethod);
						if(list!=null){
							try {
								String json = JSONObject.toJSONString(list);
								redisUtil.setCacheString(key, json);
								redisUtil.setTimeout(key, redisUtil.longTime);
							} catch (RedisSystemException e) {
								log.error("redis save error,info:{}",e);
							}
						}
					}
				}
			}
		} catch (RedisSystemException e) {
			log.error("redis read error,info:{}",e);
			list=new ArrayList<>();
		}
		return list;
	}

	@Override
	public int add(Banner banner) {
		int flag = bannerMapper.insertSelective(banner);
		if(flag>0){
			deleteRedis(banner.getShowMethod());
		}
		return flag;
	}

	@Override
	public int update(Banner banner) {
		int flag = bannerMapper.updateByPrimaryKeySelective(banner);
		if(flag > 0){
			deleteRedis(banner.getShowMethod());
		}
		return flag;
	}

	@Override
	public int delete(Integer bannerId) {
		Banner banner = bannerMapper.selectByPrimaryKey(bannerId);
		if(banner!=null){
			bannerMapper.deleteByPrimaryKey(bannerId);
			deleteRedis(banner.getShowMethod());
		}
		return 1;
	}

	private void deleteRedis(Integer type){
		try {
			String key=redis_key+type;
			redisUtil.del(key);
		} catch (RedisSystemException e) {
			log.error("redis delete error,info:{}",e);
		}
	}

	@Override
	public List<Banner> selectAll() {
		BannerExample example = new BannerExample();
		example.setOrderByClause(" show_method asc,order_no desc,id desc");
		return bannerMapper.selectByExample(example);
	}

	@Override
	public Banner selectById(Integer id) {
		return bannerMapper.selectByPrimaryKey(id);
	}
}
